package com.c2soft.ncmxReport.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;


//import org.perf4j.aop.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.c2soft.ncmxReport.service.ChartDataService;
import com.c2soft.ncmxReport.service.ChartService;

// TODO: Auto-generated Javadoc
/**
 * The Class ChartController.
 */
@Controller
@RequestMapping("/reports")
public class ChartController {

	static final Logger logger = LogManager.getLogger(ChartController.class);
	

	/** The chart service. */
	@Autowired
	ChartService chartService;
	@Autowired
	ChartDataService chartDataService;
    
	/**
	 * Draw pie chart.
	 * 
	 * @param response
	 *            the response
	 */
	@RequestMapping(value = "/piechart", method = RequestMethod.GET, produces = "image/png")
	//@Profiled(tag = "failuresSeparatelyExample", logFailuresSeparately = true)
	public void drawPieChart(HttpServletResponse response) {
		logger.debug("Entering Method drawPieChart.");
		PieDataset pdSet = createDataSet();

		JFreeChart chart = chartService.createPieChart(pdSet, "My Pie Chart");

		try {
			logger.debug("Writing Pie chart to outputStream");
			ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
					750, 400);
			response.getOutputStream().close();

		} catch (IOException ex) {

			ex.printStackTrace();
		}
		logger.debug("Exiting Method drawPieChart.");
	}

	/**
	 * Draw bar chart.
	 * 
	 * @param response
	 *            the response
	 */
	@RequestMapping(value = "/barchart", method = RequestMethod.GET, produces = "image/png")
	public void drawBarChart(HttpServletResponse response) {
		logger.debug("Entering Method drawBarChart.");

		DefaultCategoryDataset dSet = createBarDataSet();

		JFreeChart chart = chartService
				.createBarChart("Comodity cost comparision", "Comodity",
						"Cost in Rupees", dSet);

		try {
			logger.debug("Writing Bar chart to outputStream");
			ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
					750, 400);
			response.getOutputStream().close();

		} catch (IOException ex) {
			ex.printStackTrace();
		}
		logger.debug("Exiting Method drawBarChart.");

	}

	/**
	 * Draw line chart.
	 * 
	 * @param response
	 *            the response
	 */
	@RequestMapping(value = "/linechart", method = RequestMethod.GET, produces = "image/png")
	public void drawLineChart(HttpServletResponse response) {
		logger.debug("Entering Method drawLineChart.");

		XYSeriesCollection dSet = createLineDataSet();

		JFreeChart chart = chartService.createLineChart("Wheat cost in 2010",
				"Wheat sold in tons", "Cost in Rupees", dSet);

		try {
			logger.debug("Writing line chart to outputStream");
			ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
					750, 400);
			response.getOutputStream().close();

		} catch (IOException ex) {
			ex.printStackTrace();
		}
		logger.debug("Exiting Method drawLineChart.");

	}

	/**
	 * Draw area chart.
	 * 
	 * @param response
	 *            the response
	 */
	@RequestMapping(value = "/areachart", method = RequestMethod.GET, produces = "image/png")
	public void drawAreaChart(HttpServletResponse response) {
		logger.debug("Entering Method drawAreaChart.");

		XYSeriesCollection dSet = createLineDataSet();

		JFreeChart chart = chartService.createAreaChart("Wheat cost in 2010",
				"Wheat sold in tons", "Cost in Rupees", dSet);

		try {
			logger.debug("Writing Area chart to outputStream");
			ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
					750, 400);
			response.getOutputStream().close();

		} catch (IOException ex) {
			ex.printStackTrace();
		}
		logger.debug("Exiting Method drawAreaChart.");

	}

	/**
	 * Draw time chart.
	 * 
	 * @param response
	 *            the response
	 */
	@RequestMapping(value = "/timechart", method = RequestMethod.GET, produces = "image/png")
	public void drawTimeChart(HttpServletResponse response) {
		logger.debug("Entering Method drawTimeChart.");

		TimeSeriesCollection dSet = chartDataService.createTimeSeriesDataSet();

		JFreeChart chart = chartService.createTimeChart(
				"Population of Mumbai Town", "Date", "Population", dSet);

		try {
			logger.debug("Writing TimeSeries chart to outputStream");
			ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
					750, 400);
			response.getOutputStream().close();

		} catch (IOException ex) {
			ex.printStackTrace();
		}
		logger.debug("Exiting Method drawTimeChart.");

	}

	// Dummy Data methods
	/**
	 * Creates the data set.
	 * 
	 * @return the pie dataset
	 */
	private PieDataset createDataSet() {
		logger.debug("Entering method CreateDataSet for Pie Data.");
		logger.debug("Creating data for Pie Chart");
		DefaultPieDataset dpd = new DefaultPieDataset();
		dpd.setValue("SoftComodity", 59.5);
		dpd.setValue("HardComodity", 20);
		dpd.setValue("Mac", 20);
		dpd.setValue("PC", 20);
		logger.debug("Data for Pie Chart Created in object ");
		logger.debug("Exiting method  CreateDataSet.");
		return dpd;
	}

	/**
	 * Creates the bar data set.
	 * 
	 * @return the default category dataset
	 */
	private DefaultCategoryDataset createBarDataSet() {
		logger.debug("Entering method createBarDataSet for Bar Data.");
		logger.debug("Creating data for Bar Chart");
		DefaultCategoryDataset ds = new DefaultCategoryDataset();
		ds.setValue(6, "Copper", "Jane");
		ds.setValue(3, "Iron", "Jane");
		ds.setValue(7, "Copper", "Tom");
		ds.setValue(10, "Iron", "Tom");
		ds.setValue(8, "Copper", "Jill");
		ds.setValue(8, "Iron", "Jill");
		ds.setValue(5, "Copper", "John");
		ds.setValue(6, "Iron", "John");
		ds.setValue(12, "Copper", "Fred");
		ds.setValue(5, "Iron", "Fred");
		logger.debug("Data for Bar Chart Created");
		logger.debug("Exiting method createBarDataSet.");

		return ds;
	}

	/**
	 * Creates the line data set.
	 * 
	 * @return the XY series collection
	 */
	private XYSeriesCollection createLineDataSet() {
		logger.debug("Entering method createLineDataSet for Line Data.");
		logger.debug("Creating data for Line Chart");
		XYSeriesCollection ds = new XYSeriesCollection();
		XYSeries series = new XYSeries("XYGraph");
		series.add(1, 1);
		series.add(1, 2);
		series.add(2, 1);
		series.add(3, 9);
		series.add(4, 10);

		ds.addSeries(series);
		logger.debug("Data for Line Chart Created");
		logger.debug("Exiting method createLineDataSet.");
		return ds;
	}

	/**
	 * Creates the time series data set.
	 * 
	 * @return the time series collection
	 */
	/*private TimeSeriesCollection createTimeSeriesDataSet() {
		logger.debug("Entering method createTimeSeriesDataSet for TimeSeries Data.");
		logger.debug("Creating data for TimeSeries Chart");
		TimeSeriesCollection ds = new TimeSeriesCollection();
		TimeSeries pop = new TimeSeries("Population", Day.class);
		pop.add(new Day(10, 1, 2004), 100);
		pop.add(new Day(10, 2, 2004), 150);
		pop.add(new Day(10, 3, 2004), 250);
		pop.add(new Day(10, 4, 2004), 275);
		pop.add(new Day(10, 5, 2004), 325);
		pop.add(new Day(10, 12, 2004), 425);

		ds.addSeries(pop);
		logger.debug("Data for TimeSeries Chart Created");
		logger.debug("Exiting method createTimeSeriesDataSet");
		return ds;
	}*/
}
